package fightplan.persistence.xml;

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import fightplan.business.Dojo;
import fightplan.business.Fighter;
import fightplan.business.Gender;
import fightplan.business.SkrKyuFighterUtil;
import fightplan.business.Tournament;
import fightplan.core.FPExceptionHandler;
import fightplan.persistence.FilePersistence;
import fightplan.persistence.xml.kyu.Anmeldung;

public class XmlImport {

	public static boolean imprtFightersAndTeams(Tournament tournament, File[] files) {
		Tournament temp;
		// cheap clone (save und load)
		try {
			File tempFile = File.createTempFile("b4import", null);
			FilePersistence.save(tempFile, tournament, false);
			temp = FilePersistence.load(tempFile, false);
			temp.getFighters().clear();
		} catch (Exception x) {
			x.printStackTrace();
			FPExceptionHandler.instance().showWarning("Import aus technsichen Gründen fehlgeschlagen.\n",
					"Interner Fehler");
			return false;
		}

		for (File file : files) {
			try {
				imprtFightersAndTeams(temp, file);
			} catch (JAXBException e) {
				FPExceptionHandler.instance().showWarning("Import aus technsichen Gründen fehlgeschlagen.\n",
						"Interner Fehler");
				e.printStackTrace();
				return false;
			}
		}

		// do checks

		List<Fighter> fighters = temp.getFighters();
		String error = "";
		for (int i = 0; i < fighters.size(); i++) {
			String name1 = fighters.get(i).getName().trim();
			for (int j = i + 1; j < fighters.size(); j++) {
				String name2 = fighters.get(j).getName().trim();
				if (name1.equalsIgnoreCase(name2)) {
					error += "Name doppelt: " + name1 + "\n";
				}
			}
		}

		for (int i = 0; i < fighters.size(); i++) {
			String name1 = fighters.get(i).getName().trim();
			for (int j = 0; j < tournament.getFighters().size(); j++) {
				String name2 = tournament.getFighters().get(j).getName().trim();
				if (name1.equalsIgnoreCase(name2)) {
					error += "Name bereits vorhanden: " + name1 + "\n";
				}
			}
		}

		if (error.length() > 0) {
			FPExceptionHandler.instance().showWarning("Teilnehmer doppelt oder bereits vorhanden:\n\n" + error,
					"Import fehlgeschlagen");
			return false;
		} else {
			// do it again for the real tournament
			for (File file : files) {
				try {
					imprtFightersAndTeams(tournament, file);
				} catch (JAXBException e) {
					// should not happen (hasnt happened in the first try)
					e.printStackTrace();
				}
			}
			return true;
		}
	}

	private static void imprtFightersAndTeams(Tournament tournament, File file) throws JAXBException {
		JAXBContext context = JAXBContext.newInstance("fightplan.persistence.xml.kyu");

		Unmarshaller unmarshaller = context.createUnmarshaller();

		Anmeldung anmeldung = (Anmeldung) unmarshaller.unmarshal(file);
		for (Anmeldung.Dojo xmlDojo : anmeldung.getDojo()) {
			Dojo dojo = new Dojo();
			dojo.setName(xmlDojo.getName());

			for (fightplan.persistence.xml.kyu.Fighter xmlFighter : xmlDojo.getFighter()) {
				Fighter fighter = new Fighter();
				fighter.setBelt(xmlFighter.getBelt());
				fighter.setDojo(dojo);
				fighter.setGender(Gender.parse(xmlFighter.getGender()));
				fighter.setName(xmlFighter.getName());
				fighter.setWeight(xmlFighter.getWeight());
				fighter.setYear(xmlFighter.getYear());

				if (xmlFighter.isKata()) {
					SkrKyuFighterUtil.setCategory(tournament, fighter, SkrKyuFighterUtil.KATA, true);
				}
				if (xmlFighter.isKumite()) {
					SkrKyuFighterUtil.setCategory(tournament, fighter, SkrKyuFighterUtil.KUMITE, true);
				}
				if (xmlFighter.isKumiteChallange()) {
					SkrKyuFighterUtil.setCategory(tournament, fighter, SkrKyuFighterUtil.KUMITE_CHALLANGE, true);
				}

				tournament.getFighters().add(fighter);
			}

			tournament.getDojos().add(dojo);
		}
	}

}
